<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>worker</title>
</head>

<body>
    <script src="../../test/e2e/js/AssetTool.js"></script>
    <script src="../../test/e2e/js/LightBoxesTool.js"></script>
    <script src="../../test/e2e/js/PositionTool.js"></script>
    <script src="../../test/e2e/js/LightTool.js"></script>
    <script src="../../test/e2e/js/CameraTool.js"></script>
    <script src="../../test/e2e/js/LightMaterialTool.js"></script>
    <script src="../../dist/wd.js"></script>
    <script src="../../dist/wd.render.worker.js"></script>

    <script>
        window.onload = function () {
            return AssetTool.loadConfig(["../config/setting_worker.json", "../config/"], null, function () {
                return initSample(wd.unsafeGetState());
            });


            function _init1(script, api, state) {
                var scriptAttribute2 =
                    api.unsafeGetScriptAttribute(
                        script,
                        "scriptAttribute2",
                        state
                    );

                var a = api.unsafeGetScriptAttributeFieldValue(
                    "a",
                    scriptAttribute2
                );

                console.log("init->a: ", a)

                return state
            };

            function _update1(script, api, state) {
                var scriptAttribute1 =
                    api.unsafeGetScriptAttribute(
                        script,
                        "scriptAttribute1",
                        state
                    );

                var a = api.unsafeGetScriptAttributeFieldValue(
                    "a",
                    scriptAttribute1
                );

                console.log("a: ", a)

                var state =
                    api.setScriptAttributeFieldValue(
                        script,
                        [

                        "scriptAttribute1",

                        "a",

                        a + 1,
                        ],


                        state
                    );

                var gameObject =
                    api.unsafeGetScriptGameObject(
                        script, state
                    );

                var transform =
                    api.unsafeGetGameObjectTransformComponent(gameObject, state);

                var [x, y, z] =
                    api.getTransformLocalPosition(
                        transform, state
                    );



                if (x >= 20) {
                    var state = api.disposeGameObject(gameObject, state);

                    return state;
                }
                else {
                    var state =
                        api.setTransformLocalPosition(
                            transform, [x >= 20 ? -20 : x + 1, y, z], state
                        );

                    return state;
                }

                return state;


                // var state =
                //     api.setTransformLocalPosition(
                //         transform, [x >= 20 ? -20 : x + 1, y, z], state
                //     );

                // return state
            };

            function _dispose1(script, api, state) {
                console.log("script: ", script);

                return state;
            };

            function _addScript(box, state) {
                var [state, scriptComponent] = wd.createScript(state);

                // var [state, scriptEventFunctionData1] =
                var scriptEventFunctionData1 =
                    wd.createScriptEventFunctionData(
                        // "scriptEventFunctionData1"
                        {
                            "init": (script, api, state) => {
                                return _init1(script, api, state);
                            },
                            "update": (script, api, state) => {
                                return _update1(script, api, state);
                            },
                            "dispose": (script, api, state) => {
                                return _dispose1(script, api, state);
                            }
                        }
                    );


                // var scriptEventFunctionData1 =
                //     wd.addScriptEventFunctionData(
                //         "update",
                //         (script, api, state) => {
                //             return _update1(script, api, state);
                //         },

                //         scriptEventFunctionData1
                //     );



                // var [state, scriptAttribute1] =
                var scriptAttribute1 =
                    wd.createScriptAttribute(
                    );

                var scriptAttribute1 =
                    wd.addScriptAttributeFieldJsObj(
                        "a", {
                            "type": "int",
                            "defaultValue": 0
                        },
                        scriptAttribute1
                    );
                var scriptAttribute1 =
                    wd.addScriptAttributeFieldJsObj(
                        "b", {
                            "type": "float",
                            "defaultValue": 0.1
                        },
                        scriptAttribute1
                    );



                var scriptAttribute2 =
                    wd.createScriptAttribute(
                    );

                var scriptAttribute2 =
                    wd.addScriptAttributeFieldJsObj(
                        "a", {
                            "type": "float",
                            "defaultValue": 10.5
                        },
                        scriptAttribute2
                    );


                var state =
                    wd.addScriptEventFunctionData(
                        scriptComponent,
                        "scriptEventFunctionData1",
                        scriptEventFunctionData1,
                        state

                    );



                var state =
                    wd.addScriptAttribute(

                        scriptComponent,

                        "scriptAttribute1",
                        scriptAttribute1,

                        state
                    );


                var state =
                    wd.addScriptAttribute(

                        scriptComponent,

                        "scriptAttribute2",
                        scriptAttribute2,

                        state
                    );


                var state =
                    wd.addGameObjectScriptComponent(
                        box, scriptComponent, state
                    );

                return state;
            };

            function initSample(state) {
                var [state, box] = LightBoxesTool.createBox(state);


                var state = wd.setAmbientLightColor([0.2, 0.2, 0.2], state);


                var [state, directionLightGameObject] = LightTool.createDirectionLight(state);

                var transform = wd.unsafeGetGameObjectTransformComponent(directionLightGameObject, state);

                var state = wd.setTransformLocalEulerAngles(transform, [0, 180, 0], state);




                var [state, cameraGameObject] = LightBoxesTool.createCamera(state);



                var [state, cameraController] = wd.createArcballCameraController(state);

                var state =
                    wd.setArcballCameraControllerDistance(cameraController, 50, state);



                var state =
                    wd.setArcballCameraControllerWheelSpeed(cameraController, 1, state);

                var state = wd.addGameObjectArcballCameraControllerComponent(cameraGameObject, cameraController, state);


                var state =
                    wd.bindArcballCameraControllerEvent(
                        cameraController, state
                    );

                var state = _addScript(box, state);

                var [state, newBoxes] = wd.cloneGameObject(box, 2, true, state);


                var flatten = (arr) => {
                    return arr.reduce((a, b) => {
                        var arr = a.concat(b);
                        return arr;
                    }, []);
                };
                var newBoxes = flatten(newBoxes);


                wd.startDirector(state);
            }
        };
    </script>
</body>

</html>